package com.edz.from0507to0513apk.Accountsave.frag;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.edz.from0507to0513apk.R;
import com.edz.from0507to0513apk.Util.DensityUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MsgFrag extends Fragment {
    private View view_msg;
    @BindView(R.id.view_pager)
    ViewPager view_pager;
    @BindView(R.id.magic_indicator)
    MagicIndicator magic_indicator;

    private String[] titles = new String[]{"普通", "加密", "超密"};
    private int flag = 0;      //信息类型

    private NorMsgFrag norMsgFragment;
    private EncMsgFrag encMsgFragment;
    private UtrMsgFrag utrMsgFragment;

    public MsgFrag() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        // Inflate the layout for this fragment
        view_msg = inflater.inflate(R.layout.fragment_msg, container, false);
        ButterKnife.bind(this, view_msg);
        initView();
        return view_msg;
    }

    private void initView() {
        initMagicIndicator();
        initViewPager();
    }

    private void initMagicIndicator() {
        magic_indicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdjustMode(true);  //ture 即标题平分屏幕宽度的模式
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles == null ? 0 : titles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(titles[index]);
                int size = (int) getResources().getDimension(R.dimen.w_14_dip);
                simplePagerTitleView.setTextSize(DensityUtil.px2sp(context, size));
                simplePagerTitleView.setNormalColor(Color.GRAY);
                simplePagerTitleView.setSelectedColor(Color.BLACK);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        view_pager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
//                BezierPagerIndicator indicator = new BezierPagerIndicator(context);
//                indicator.setColors(Color.parseColor("#ff4a42"), Color.parseColor("#fcde64"),
// Color.parseColor("#73e8f4"), Color.parseColor("#76b0ff"), Color.parseColor("#c683fe"));
//                return indicator;
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setColors(R.color.app_color_blue);
                return linePagerIndicator;
            }
        });
        magic_indicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magic_indicator, view_pager);
    }

    private void initViewPager() {

        //ViewPager设置适配器
        view_pager.setOffscreenPageLimit(0);
        view_pager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager()));

        Intent intent = getActivity().getIntent();
        flag = intent.getIntExtra("flag", 0);
        Log.e("test current item ===", flag + "");
        view_pager.setCurrentItem(flag);
    }

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        public MyFragmentPagerAdapter(FragmentManager childFragmentManager) {
            super(childFragmentManager);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    if (norMsgFragment == null) {
                        norMsgFragment = new NorMsgFrag();
                    }
                    return norMsgFragment;
                case 1:
                    if (encMsgFragment == null) {
                        encMsgFragment = new EncMsgFrag();
                    }
                    return encMsgFragment;
                case 2:
                    if (utrMsgFragment == null) {
                        utrMsgFragment = new UtrMsgFrag();
                    }
                    return utrMsgFragment;
                default:
                    return null;
            }
        }

    }
}
